package it.trekker.dao;

import java.io.Serializable;
import javax.persistence.Embeddable;

@Embeddable
public class GPSCoordinate implements Serializable {
	
	protected double latitude;
	protected double longitude;
	protected double altitude;
	
	public GPSCoordinate() {
	}
	
	public GPSCoordinate(GPSCoordinate coord) {
		if(coord != null) {
			this.latitude = coord.latitude;
			this.longitude = coord.longitude;
			this.altitude = coord.altitude;
		}
	}
	
	public double getLatitude() {
		return latitude;
	}
	
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	
	public double getLongitude() {
		return longitude;
	}
	
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	
	public double getAltitude() {
		return altitude;
	}
	
	public void setAltitude(double altitude) {
		this.altitude = altitude;
	}
	
	public static double distanceBetween(GPSCoordinate coord1, GPSCoordinate coord2) {
		int R = 6371;	// Means earth radius for Km result
		double distance;
		// To radians
		double lat1 = Math.toRadians(coord1.latitude);
		double lon1 = Math.toRadians(coord1.longitude);
		double lat2 = Math.toRadians(coord2.latitude);
		double lon2 = Math.toRadians(coord2.longitude);
		
		distance = 	Math.acos(
						Math.sin(lat1) * Math.sin(lat2) +
						Math.cos(lat1) * Math.cos(lat2) *
						Math.cos(lon2 - lon1)
					) * R;
		
		return distance;
	}
	
	public static double degreesToRadians(double degrees) {
		return (degrees * Math.PI / 180.0);
	}
	
	@Override
	public String toString() {
		return String.format("lat: %f - long: %f - alt: %f", 
				latitude, longitude, altitude);
	}
	
	@Override
	public boolean equals(Object o) {
		if(o == null)
			return false;
		if(this == o)
			return true;
		if(!(o instanceof GPSCoordinate))
			return false;
		
		GPSCoordinate other = (GPSCoordinate) o;
		
		if(this.longitude != other.longitude)
			return false;
		if(this.latitude != other.latitude)
			return false;
		if(this.altitude != other.altitude)
			return false;
		
		return true;
	}
}
